var QS_importDialog = null;
var officePortfolio = function(){
    this.El;
    this.URL='portfolio.jsp?';
    this.portfolioListData=[];
    this.dataGrid=null;
    this.win=null;
    this.w=400;
    this.h=236;
    this.isOpened=false;
    this.wlObj = null;
    this.type = "office";
	
    this.init();
};
officePortfolio.prototype = {
    init:function(){
    	this.tValidation = new tickerValidation(0);
    },
    _getData:function(){
        var self = this;
        var rid;
        $.get(this.URL+"action="+this.type+"InvestmentList&tmpid="+ QS_UserTID,null,function(data){
            var d = data.split("|"), temp = [];
            self.portfolioListData = eval("(" + data + ")").Portfolios;
            var moreData = [];
            for(var i =0;i<self.portfolioListData.length;i++){
                moreData.push({cmd:self.portfolioListData[i].PfId,text:self.portfolioListData[i].Name});
            }
            workspaceManager.openOpenFileWin({
                    'Investment': moreData
                }, function(el){
                    self._inputInvesetmentName(el.getAttribute('cmd'));
                }, null, null);
        });
    },
    _inputInvesetmentName: function(id){
		var self = this;
		Util.prompt("Please enter a name", function(t){
			if (t == null || t == "") {
				self._inputInvesetmentName(id);
			}
			else 
				if (!QS_Validator.test(t, 'name')) {
					self._inputInvesetmentName(id);
				}
				else {
					self._getInvertmentListData(id, t);
				}
		});
	},
	
    _getInvertmentListData:function(id,name){
        var self = this;
        $.get(self.URL + "action=" + self.type + "InvestmentTickList&tmpid=" + QS_UserTID + "&pflId=" + id, null, function(data){
            var arr1 = eval("(" + data + ")").Holdings;
            var allTickerPids = [];
            for (var i = 0; i < arr1.length; i++) {
                allTickerPids.push(arr1[i].SecId);
            }
            workspaceManager.OpenFileWin.hide();
//            if(allTickerPids.length>300){
//                allTickerPids = allTickerPids.slice(0,300);
//            }
            self.tValidation.checkTickers(allTickerPids, function(oo){
				var maxTickers = QS_watchlistDataManager.getMaxTickers();
                if (oo.length >= maxTickers) {
                    Util.message("Security limit is "+maxTickers+". Please contact your sales representative if you are interested in increasing the limit.");
                }
                var temp = [];
				
                var d = new Date();
				var year = d.getFullYear();
		        var month = d.getMonth() + 1;
		        month = month < 10 ? ('0' + month) : month;
		        var day = d.getDate();
		        day = day < 10 ? ('0' + day) : day;
                var dstr =(month + '-' + day + '-' + year) ;
				
				//a.getFullYear() + "/" + a.getMonth() + "/" + a.getDate();
				var len=oo.length<=maxTickers?oo.length:maxTickers;
                for (var i = 0; i < len; i++) {
					temp.push({
						Action: 1,
						Quantity: 0,
						Name: '',
						SecId: gData[oo[i]].performanceId ,
						HoldingId: '',
						PurchasePrice: 0,
						Comm: 0,
						Date: dstr ,
						Rank:  i 
					});
				}
                self._saveWatchList(temp,name);
            });
        });
    },
	
	_saveWatchList: function(list, name){
		var self = this;
		QS_watchlistDataManager.createWatchList(name, '1', list, function(args){
			var pid = args["portfolioId"];
			QS_watchlistDataManager.saveHoldings(pid,null);
			if (self.wlObj.openPortfolio) {
				self.wlObj.openPortfolio(pid, '', null, true);
			}
		});
		//this.win.hideLoading();
	},
	
    open: function(obj, type){
        if (!GOLBAL_DATA.IsSeamLessLogin || type.toLowerCase() != GOLBAL_DATA.SSOFrom.toLowerCase()) {
            Util.alert('Please launch QuoteSpeed from ' + (type.toLowerCase()=='direct'?'Direct':'Office') + ' to import your investment lists.');
            return;
        }
        this.wlObj = obj;
        this.type = type;
        this._getData();
    }
};